/*
    The Projectile class, specific to this project, manages Entities that move in some fashion,
    and can collide and do something to a ship.
*/

#ifndef PROJECTILE
#define PROJECTILE

#include "Class Manager.h"
#include "Dark.h"

class Ship;

Ship *BJ_TEMP_SHIP = NULL;

class Projectile
{
    public :

        Projectile
        (
            int
        )
        {
        
        }
        
        void Init
        (
            double iXLoc         =   0.0,
            double iYLoc         =   0.0,
            double iXVelocity    =   0.0,
            double iYVelocity    =   0.0,
            double iRotation     =   0.0,
            int    iTime         =   0  ,// in ticks
            double iRadius       =   0.0,
            double iMass         =   0.0,
            double iDamage       =   0.0,
            int    iImage        =   0  ,
            Ship  *iPOwner       =  NULL
            
        )
        {
            _location.set(iXLoc,iYLoc);
            _pImage        = iImage;
            _xVelocity    = iXVelocity;
            _yVelocity    = iYVelocity;
            _rotation     = iRotation;
            _time         = iTime;
            _mass         = iMass;
            _radius       = iRadius;
            _damage       = iDamage;
            _spriteId     = Dark::GetFreeSpriteId();
            _owner        = iPOwner;
            dbSprite(_spriteId, iXLoc, iYLoc,_pImage);
            dbOffsetSprite(_spriteId,iRadius,iRadius);
            dbSizeSprite(_spriteId,iRadius*2,iRadius*2);
        }

        ~Projectile
        (
            void
        )
        {
            dbDeleteSprite(_spriteId);
        }
          
        bool refresh
        (
            void
        )
        {
            _location.set(_location.x() + (_xVelocity*Dark::PERIOD),_location.y() + (_yVelocity*Dark::PERIOD));
            _time--;
            if (_time <= 0)
            {
                return true;
            }
            else
            {
            
            }
            dbSprite(_spriteId, _location.x(), _location.y(),_pImage);
            dbRotateSprite(_spriteId,_rotation+90);
            return false; 
        }
        

        friend bool ProjectileInRange(Projectile*);        
    private:
        int    _pImage    ;
        int    _spriteId ;
        Point  _location ;
        double _xVelocity;
        double _yVelocity;
        double _rotation ;
        double _mass     ;
        int    _time     ;
        double _radius   ;
        double _damage   ;
        Ship*  _owner    ;
};
ObjectManager<Projectile> ProjectileManager;

bool ProjectileInRange
(
    Projectile *it
)
{
    if (it->_owner == BJ_TEMP_SHIP)
    {
        return false;
    }
    double distance = sqrt(it->_location.distance_sqr(BJ_gTempLoc)) - (BJ_gR+it->_radius);
    if (distance < 0)
    {
        it->_time = 0;
        BJ_gX += it->_xVelocity * it->_mass;
        BJ_gY += it->_yVelocity * it->_mass;
        BJ_gD += it->_damage;
        //return it->Refresh();
    }
    return false;
}

#endif